/*
 *  Jajuk
 *  Copyright (C) 2003-2008 The Jajuk Team
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *  $Revision: 3132 $
 */
package aspecto;

import ext.services.lastfm.AudioScrobblerAlbum;
import ext.services.lastfm.AudioScrobblerService;
import ext.services.lastfm.AudioScrobblerTrack;

import java.awt.Color;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jajuk.base.*;
import org.jajuk.util.*;
import org.jajuk.util.log.Log;
import org.jajuk.ui.perspectives.*;
import org.jajuk.ui.views.*;
import org.jajuk.ui.wizard.*;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jajuk.ui.thumbnails.*;
import org.jajuk.util.error.CannotRenameException;
import org.jajuk.util.error.NoneAccessibleFileException;
import org.jajuk.util.filters.*;
import org.jajuk.util.Messages;
import org.jajuk.util.error.CannotRenameException;
import org.jajuk.util.error.NoneAccessibleFileException;


/*
 * 
 */
privileged public aspect MessAspect {

  /**
   * Get item description
   */
  public String Album.getDesc() {
    return Messages.getString("Item_Album") + " : " + getName2();
  }
  
  /**
   * Get item description
   */
  public String Author.getDesc() {
    return Messages.getString("Item_Author") + " : " + getName2();
  }
  
  /**
   * Get item description
   */
  public String Device.getDesc() {
    return Messages.getString("Item_Device") + " : " + getName();
  }
  
  /**
   * Get item description
   */
  public String Directory.getDesc() {
    String sName = null;
    if (getParentDirectory() == null) {
      sName = getDevice().getUrl();
    } else {
      sName = getFio().getAbsolutePath();
    }
    return Messages.getString("Item_Directory") + " : " + sName;
  }
  
  /**
   * Get item description
   */
  public String File.getDesc() {
    return Messages.getString("Item_File") + " : " + getName();
  }
  
  /**
   * Get item description
   */
  public String Playlist.getDesc() {
    return Messages.getString("Item_Playlist_File") + " : " + getName();
  }
  
  /**
   * Get item description
   */
  public String Style.getDesc() {
    return Messages.getString("Item_Style") + " : " + getName2();
  }
  
  /**
   * Get item description
   */
  public String Track.getDesc() {
    return Messages.getString("Item_Track") + " : " + getName();
  }
  
  /**
   * Get item description
   */
  public String Type.getDesc() {
    return Messages.getString("Type") + " : " + getName();
  }
  
  /**
   * Get item description
   */
  public String Year.getDesc() {
    return Messages.getString("Property_year") + " : " + getName();
  }
  
  public String AlbumsPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Albums");
  }
  
  public String ConfigurationPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Configuration");
  }
  
  public String DisplayPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Display");
  }
  
  public String FilesPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Files");
  }
  
  public String InfoPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Info");
  }
  
  public abstract String IPerspective.getDesc();
  
  public String PlaylistsPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Playlists");
  }
  
  public String SimplePerspective.getDesc() {
    return Messages.getString("Perspective_Description_Simple");
  }
  
  public String StatPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Statistics");
  }
  
  public String TracksPerspective.getDesc() {
    return Messages.getString("Perspective_Description_Tracks");
  }
  
  public String AlbumsTableView.getDesc() {
    return Messages.getString("AlbumsTableView.0");
  }
  
  public String AnimationView.getDesc() {
    return Messages.getString("AnimationView.0");
  }
  
  public String CatalogView.getDesc() {
    return Messages.getString("CatalogView.0");
  }
  
  public String CDScanView.getDesc() {
    return Messages.getString("CDScanView.12");
  }
  
  public String CoverView.getDesc() {
    return Messages.getString("CoverView.3");
  }
  
  public String DeviceView.getDesc() {
    return Messages.getString("DeviceView.23");
  }
  
  public String FilesTableView.getDesc() {
    return Messages.getString("FilesTableView.0");
  }
  
  public String FilesTreeView.getDesc() {
    return Messages.getString("FilesTreeView.0");
  }
  
  public abstract String IView.getDesc();
  
  public String LyricsView.getDesc() {
    return Messages.getString("LyricsView.0");
  }
  
  public String NewsView.getDesc() {
    return Messages.getString("NewsView.0");
  }
  
  public String ParameterView.getDesc() {
    return Messages.getString("ParameterView.87");
  }
  
  public String PlaylistView.getDesc() {
    return Messages.getString("AbstractPlaylistEditorView.15");
  }
  
  public String QueueView.getDesc() {
    return Messages.getString("QueueView.0");
  }
  
  public String StatView.getDesc() {
    return Messages.getString("StatView.16");
  }
  
  public String SuggestionView.getDesc() {
    return Messages.getString("SuggestionView.0");
  }
  
  public String TracksTableView.getDesc() {
    return Messages.getString("TracksTableView.0");
  }
  
  public String TracksTreeView.getDesc() {
    return Messages.getString("TracksTreeView.0");
  }
  
  public String WikipediaView.getDesc() {
    return Messages.getString("WikipediaView.0");
  }
  
  public String AboutWindow.getDesc() {
    return Messages.getString("AboutView.10");
  }
  
  public String HelpWindow.getDesc() {
    return Messages.getString("HelpView.2");
  }
  
  public abstract String Item.getDesc();
  
  public String AudioScrobblerAlbumThumbnail.getDescription() {
    // populate album detail
    if (album.getTracks() == null) {
      AudioScrobblerAlbum album = AudioScrobblerService.getInstance().getAlbum(
          this.album.getArtist(), this.album.getTitle());
      if (album != null) {
        this.album = album;
      }
    }
    Color bgcolor = SubstanceLookAndFeel.getActiveColorScheme().getUltraLightColor();
    Color fgcolor = SubstanceLookAndFeel.getActiveColorScheme().getForegroundColor();
    String sOut = "<html bgcolor='#" + Util.getHTMLColor(bgcolor) + "'><TABLE color='"
        + Util.getHTMLColor(fgcolor) + "'><TR><TD VALIGN='TOP'> <b>" + "<a href='file://" + XML_URL
        + '?' + album.getUrl() + "'>" + album.getTitle() + "</a>" + "</b><br><br>";
    // display cover
    sOut += "<img src='" + album.getCoverURL() + "'><br>";
    // Display author as global value only if it is a single author album
    // We use file://<item type>?<item id> as HTML hyperlink format
    sOut += "<br>" + Messages.getString("Property_author") + " : " + "<a href='file://" + XML_URL
        + '?' + album.getArtistUrl() + "'>" + album.getArtist() + "</a>";
    // Display year if available
    String year = album.getYear();
    if (!Util.isVoid(year)) {
      sOut += "<br>" + Messages.getString("Property_year") + " : " + year;
    }
    sOut += "</TD><TD>";
    // Show each track detail if available
    if (album.getTracks() != null) {
      for (AudioScrobblerTrack track : album.getTracks()) {
        sOut += "<b>" + "<a href='file://" + XML_URL + '?' + track.getUrl() + "'>"
            + track.getTitle() + "</a></b><br>";
      }
    }
    sOut += "</TD></TR></TABLE></html>";
    return sOut;
  }
  
  public String AudioScrobblerAuthorThumbnail.getDescription() {
    Color bgcolor = SubstanceLookAndFeel.getActiveColorScheme().getUltraLightColor();
    Color fgcolor = SubstanceLookAndFeel.getActiveColorScheme().getForegroundColor();
    String sOut = "<html bgcolor='#" + Util.getHTMLColor(bgcolor) + "'><TABLE color='"
        + Util.getHTMLColor(fgcolor) + "'><TR><TD VALIGN='TOP'> <b>" + "<a href='file://" + XML_URL
        + '?' + author.getUrl() + "'>" + author.getName() + "</a>" + "</b><br><br>";
    // display picture
    sOut += "<img src='" + author.getImageUrl() + "'></TD>";
    // Show each album for this Author
    ArrayList<AudioScrobblerAlbum> albums = AudioScrobblerService.getInstance().getAlbumList(
        author.getName());
    if (albums != null && albums.size() > 0) {
      sOut += "<TD>";
      for (AudioScrobblerAlbum album : albums) {
        sOut += "<b>";
        if (!Util.isVoid(album.getYear())) {
          sOut += album.getYear() + " ";
        }
        sOut += "<a href='file://" + XML_URL + '?' + album.getUrl() + "'>" + album.getTitle()
            + "</a>" + "</b><br>";
      }
    }
    sOut += "</TD></TR></TABLE></html>";
    return sOut;
  }
  
  public String LocalAlbumThumbnail.getDescription() {
    String size = "200x200";
    ThumbnailManager.refreshThumbnail(album, size);
    java.io.File cover = Util.getConfFileByPath(FILE_THUMBS + '/' + size + '/' + album.getID()
        + '.' + EXT_THUMB);
    List<Track> tracks = new ArrayList<Track>(TrackManager.getInstance().getAssociatedTracks(album));
    Collections.sort(tracks, new TrackComparator(TrackComparator.ORDER));
    Track firstTrack = tracks.iterator().next();
    Color bgcolor = SubstanceLookAndFeel.getActiveColorScheme().getUltraLightColor();
    Color fgcolor = SubstanceLookAndFeel.getActiveColorScheme().getForegroundColor();
    String sOut = "<html bgcolor='#" + Util.getHTMLColor(bgcolor) + "'><TABLE color='"
        + Util.getHTMLColor(fgcolor) + "'><TR><TD VALIGN='TOP'> <b>" + album.getName2()
        + "</b><br><br>";
    // display cover
    if (cover.exists()) {
      sOut += "<img src='file:" + cover.getAbsolutePath() + "'><br>";
    }
    // Display author as global value only if it is a single author album
    // We use file://<item type>?<item id> as HTML hyperlink format
    if (album.getAuthor() != null) {
      sOut += "<br>" + Messages.getString("Property_author") + ": <a href='file://" + XML_AUTHOR
          + '?' + firstTrack.getAuthor().getID() + "'>" + firstTrack.getAuthor().getName2()
          + "</a>";
    }
    // Display style
    if (album.getStyle() != null) {
      sOut += "<br>" + Messages.getString("Property_style") + ": <a href='file://" + XML_STYLE
          + '?' + firstTrack.getStyle().getID() + "'>" + firstTrack.getStyle().getName2() + "</a>";
    }
    // Display year
    if (album.getYear() != null) {
      sOut += "<br>" + Messages.getString("Property_year") + ": <a href='file://" + XML_YEAR + '?'
          + firstTrack.getYear().getID() + "'>" + firstTrack.getYear().getName() + "</a>";
    }
    // display rating (average of each track rating)
    try {
      long lRate = album.getRate();
      sOut += "<br>"
          + Messages.getString("Property_rate")
          + ": <img src='"
          + Util.getConfFileByPath(
              "cache/internal/star" + Util.getAlbumStarsNumber(lRate) + "_16x16.png").toURL()
              .toExternalForm() + "'>";
    } catch (MalformedURLException e) {
      Log.error(e);
    }
    // Compute total length in secs
    long length = album.getDuration();
    sOut += "<br>" + Messages.getString("Property_length") + ": "
        + Util.formatTimeBySec(length, false) + "</TD><TD VALIGN='TOP'><br>";

    // Show each track detail
    for (Track track : tracks) {
      sOut += "<br>";
      if (track.getOrder() > 0) {
        sOut += Util.padNumber(track.getOrder(), 2) + ": ";
      }
      sOut += "<b>" + "<a href='file://" + XML_TRACK + '?' + track.getID() + "'>" + track.getName()
          + "</a>" + " (";
      sOut += Util.formatTimeBySec(track.getDuration(), false) + ") </b>";
      if (album.getYear() == null && track.getYear().getValue() != 0) {
        sOut += " - " + track.getYear().getValue() + "   ";
      }
      // Show author if known and if it is not already shown at album
      // level
      if (album.getAuthor() == null
          && !track.getAuthor().getName2().equals(Messages.getString(UNKNOWN_AUTHOR))) {
        sOut += " - " + track.getAuthor().getName2() + "   ";
      }
    }
    sOut += "</TD></TR></TABLE></html>";
    return sOut;
  }
  
  public String AmbienceWizard.AmbiencePanel.getDescription() {
    return Messages.getString("DigitalDJWizard.47");
  }
  
  public String DigitalDJWizard.TypeSelectionPanel.getDescription() {
    return Messages.getString("DigitalDJWizard.0");
  }
  
  public String WebRadioWizard.RadioPanel.getDescription() {
    return Messages.getString("RadioWizard.0");
  }
  
  public String DigitalDJWizard.RemovePanel.getDescription() {
    return Messages.getString("DigitalDJWizard.40");
  }
  
  public String DigitalDJWizard.ChangePanel.getDescription() {
    return Messages.getString("DigitalDJWizard.44");
  }
  
  public String DigitalDJWizard.ActionSelectionPanel.getDescription() {
    return Messages.getString("DigitalDJWizard.16");
  }
  
  public String DigitalDJWizard.GeneralOptionsPanel.getDescription() {
    return Messages.getString("DigitalDJWizard.49");
  }

  public String DigitalDJWizard.TransitionsPanel.getDescription() {
    return Messages.getString("DigitalDJWizard.52");
  }
  
  public String DirectoryFilter.getDescription() {
    return Messages.getString("Item_Directory");
  }
  
  public String WebRadioWizard.RadioPanel.getName() {
    return Messages.getString("RadioWizard.1");
  }
  
  public String DigitalDJWizard.TypeSelectionPanel.getName() {
    return Messages.getString("DigitalDJWizard.46");
  }
  
  public String AmbienceWizard.AmbiencePanel.getName() {
    return Messages.getString("DigitalDJWizard.57");
  }
  
  pointcut messagesCannotRename(CannotRenameException cre):
    handler(CannotRenameException) && args(cre);

  pointcut messagesNoneAcessibleFile(NoneAccessibleFileException none):
    handler(NoneAccessibleFileException) && args(none);

  before(CannotRenameException cre) : messagesCannotRename(cre){
    Messages.showErrorMessage(cre.getCode());
  }
  before(NoneAccessibleFileException none) : messagesNoneAcessibleFile(none){
    Messages.showErrorMessage(none.getCode());
  }

}
